var _ = require('underscore');
_.mixin(require('./underscore.mixin'));
var fs = require('fs');
var appsConfig = require('../apps/' + _.get_group_name() + '/config.js');

module.exports = {
    getRequireConfig: function (path) {
        var result = {
            paths: {},
            shim: {}
        };
        if (_.empty(path))
            path = '.';
        var vendorPath = path + 'vendor';
        var requireConfig = appsConfig.require;
        _.defaults(appsConfig.require, {
            jquery: {
                path: '__VENDOR__/jquery-1.11.0',
                exports: '$'
            },
            text: {
                path: '__VENDOR__/require/text'
            }
        });
        _.each(appsConfig.require, function (val, key) {
            result.paths[key] = _.str_replace(
                ['__PATH__', '__VENDOR__'], [path, vendorPath],
                val.path
            );
            result.shim[key] = _.omit(val, ['path']);
            _.each(result.shim[key].deps, function (v, k) {
                if (_.trim(v) == '$') {
                    result.shim[key].deps[k] = 'jquery';
                }
            });
        });
        _.extend(result.paths, {
            "apps":   path + "apps",
            "build":  path + "build",
            "common": path + "common",
            "debug":  path + "debug/" + _.get_group_name(),
            "group":  path + "apps/" + _.get_group_name()
        });
        return result;
    },
    getGlobal: function () {
        return {
            groupRules: appsConfig.group,
            groupName: _.get_group_name(),
            buildTime: _.date('YmdHis'),
            isBuild: process.argv[2] == 'build',
            entryPrefix: 'group_'
        };
    }
};